package com.zhenzhang.myblog.service;

import com.zhenzhang.myblog.dao.CommentDao;
import com.zhenzhang.myblog.entity.Comment;
import com.zhenzhang.myblog.entity.MultiPageInfo;
import com.zhenzhang.myblog.entity.Reply;
import com.zhenzhang.myblog.entity.role.User;
import com.zhenzhang.myblog.exception.JsonException;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Service
public class CommentService {
    @Resource
    private CommentDao commentDao;

    public MultiPageInfo<Comment> getCommentList(Integer aid, Integer uid, int page, int size) {
        validPageNum(page,size);
        int offset = size * (page - 1);
        MultiPageInfo<Comment> res = new MultiPageInfo<>();
        res.setPage(page);
        res.setSize(size);
        // 传入aid 根据文章id获取评论列表
        if(aid != null) {
            res.setTotal(commentDao.getCommentListCountByAid(aid));
            List<Comment> comment_list = commentDao.getCommentListByAid(aid,size,offset);
            res.setList(comment_list);
        // 传入uid 根据用户id获取评论列表 若传入aid和uid aid优先级更高
        }else if(uid != null){
            res.setTotal(commentDao.getCommentListCountByUid(uid));
            List<Comment> comment_list = commentDao.getCommentListByUid(uid,size,offset);
            res.setList(comment_list);
        } else {
            throw new JsonException(400,"id参数缺失");
        }
        return res;
    }

    public MultiPageInfo<Reply> getReplyList(Integer cid, Integer uid, int page, int size) {
        validPageNum(page,size);
        int offset = size * (page - 1);
        MultiPageInfo<Reply> info = new MultiPageInfo<>();
        info.setPage(page);
        info.setSize(size);
        if(cid != null) {
            info.setTotal(commentDao.getReplyListCountByCid(cid));
            List<Reply> reply_list = commentDao.getReplyListByCid(cid,size,offset);
            System.out.println(reply_list);
            info.setList(reply_list);
        }else if(uid != null){
            info.setTotal(commentDao.getReplyListCountByUid(uid));
            List<Reply> reply_list = commentDao.getReplyListByUid(uid,page,offset);
            info.setList(reply_list);
        }else {
            throw new JsonException(400,"id参数缺失");
        }
        return info;
    }

    public void addComment(Comment comment) {
        if (comment.getAid() == null) {
            throw new JsonException(400,"id参数缺失");
        }
        commentDao.addComment(comment);
    }

    public void addReply(Reply reply) {
        if (reply.getCid() == null) {
            throw new JsonException(400,"id参数缺失");
        }
        commentDao.addReply(reply);
    }

    public void deleteComment(Integer id,Integer uid, HttpSession session) {
        User user = ((User) session.getAttribute("loginUser"));
        Integer userId = user.getId();
        if(!userId.equals(uid) && !user.getAdmin()) {
            throw new JsonException(403,"用户权限不足!");
        }
        commentDao.deleteComment(id);
    }

    public void batchDeleteComment(ArrayList<Integer> list) {
        if (list.size() == 0) {
            throw new JsonException(400, "您未选择评论");
        }
        commentDao.batchDeleteComment(list);
    }

    public void deleteReply(Integer id, Integer uid, HttpSession session) {
        User user = ((User) session.getAttribute("loginUser"));
        Integer userId = user.getId();
        if(!userId.equals(uid) && !user.getAdmin()) {
            throw new JsonException(403,"用户权限不足!");
        }
        commentDao.deleteReply(id);
    }

    public void batchDeleteReply(ArrayList<Integer> list) {
        if (list.size() == 0) {
            throw new JsonException(400,"您未选择回复");
        }
        commentDao.batchDeleteReply(list);
    }

    /**
     * 判断页数和每页大小是否合法
     * @param page  页数
     * @param size  每页大小
     */
    public void validPageNum(int page, int size) {
        // 判断传入的page和size是否合法
        if (page < 1) {
            throw new IllegalArgumentException("无效的页数" + page + "，小于1");
        } else if (size < 1) {
            throw new IllegalArgumentException("无效的页大小" + size + "，小于1");
        }
    }
}
